package com.itheima.service;

import com.itheima.admin.AdminPage;
import com.itheima.admin.employeepojo.vo.EmployeeByIdVo;
import com.itheima.admin.employeepojo.vo.EmployeePageVo;
import com.itheima.common.JsonResult;
import com.itheima.common.TokenHolder;
import com.itheima.mapper.admin.EmployeeMapper;
import com.itheima.admin.employeepojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author wuzhiyong
 */
@Service
public class EmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    /**
     * 登陆
     *
     * @param employee employee
     * @return JsonResult
     */
    public JsonResult<Object> login(Employee employee) {
        //获取密码并通过md5加密
        String password = employee.getPassword();
        String md5password = DigestUtils.md5DigestAsHex(password.getBytes());
        //设置加密后的密码
        employee.setPassword(md5password);
        Login login = employeeMapper.login(employee);
        if (login == null) {
            return JsonResult.error("用户名不存在/密码错误");
        }
        if (login.getStatus() == 0) {
            return JsonResult.error("用户已被禁用");
        } else {
            //获取token并存入redis并登陆
            String s = System.currentTimeMillis() + employee.getPassword();
            String token = DigestUtils.md5DigestAsHex(s.getBytes());
            redisTemplate.opsForValue().set(token, login.getId(), 7, TimeUnit.DAYS);
            login.setToken(token);
            return JsonResult.success(login, "登陆成功");
        }
    }

    /**
     * 登出
     *
     * @param request request
     * @return JsonResult
     */
    public JsonResult<String> logout(HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        if (token != null && !"".equals(token)) {
            redisTemplate.delete(token);
        }
        return JsonResult.success("退出成功", null);
    }

    /**
     * 分页查询
     *
     * @param name     name
     * @param page     page
     * @param pageSize pageSize
     * @return adminPage
     */
    public AdminPage<EmployeePageVo> getPage(String name, Integer page, Integer pageSize) {
        //获取索引startIndex
        int startIndex = (page - 1) * pageSize;
        //获取总条数
        int total = employeeMapper.selectAllEmployee();
        //获取分页后内容
        List<EmployeePageVo> selectpage = employeeMapper.selectPage(name, startIndex, pageSize);
        //组装响应实体
        AdminPage<EmployeePageVo> adminPage = new AdminPage<>();
        adminPage.setSize(pageSize);
        adminPage.setCurrent(page);
        adminPage.setTotal(total);
        adminPage.setRecords(selectpage);
        return adminPage;
    }

    /**
     * 根据id查询
     *
     * @param id id
     * @return EmployeeByIdVo
     */
    public EmployeeByIdVo getByIdEmployee(String id) {
        return employeeMapper.selectByIdEmployee(id);
    }

    /**
     * 新增员工
     *
     * @param employee employee
     * @return true false
     */
    public boolean addEmployee(Employee employee) {
        String username = employeeMapper.selectByUsername(employee.getUsername());
        if (username == null) {
            int insert = employeeMapper.insertEmployee(employee);
            return insert > 0;
        }
        return false;
    }

    /**
     * 启用/禁用/修改
     *
     * @param employee employee
     * @return true false
     */
    public boolean modifyEmployee( Employee employee) {
return employeeMapper.updateEmployee(employee)>0;
    }
}
